#include "T_Widget.h"
#include "ElaPushButton.h"
#include "ElaLineEdit.h"
#include "ElaText.h"

#include "ElaMessageBar.h"
#include <QDir>
#include <QDebug>
#include <QTextStream>
#include <QImage>
#include <QFile>
#include <QVBoxLayout>
T_Widget::T_Widget(ElaWidget *parent)
    : ElaWidget{parent}
{
    setFixedSize(300,200);
    setWindowTitle("选择病人");
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    QHBoxLayout *nameLayout = new QHBoxLayout();
    contentName = new ElaLineEdit(this);
    contentName->setFixedWidth(145);
    ElaText *name =new ElaText("病人姓名",this);
    name->setTextStyle(ElaTextType::TextStyle::Subtitle);
    nameLayout->addStretch();
    nameLayout->addWidget(name);
    nameLayout->addSpacing(5);
    nameLayout->addWidget(contentName);
    nameLayout->addStretch();

    QHBoxLayout *idLayout = new QHBoxLayout();
    contentId = new ElaComboBox(this);
    contentId ->setFixedWidth(150);
    ElaText *id =new ElaText("病人编号",this);
    id->setTextStyle(ElaTextType::TextStyle::Subtitle);
    idLayout->addStretch();
    idLayout->addWidget(id);
    idLayout->addWidget(contentId);
    idLayout->addStretch();

    QHBoxLayout *btnLayout = new QHBoxLayout();
    ElaPushButton *btnConfirm = new ElaPushButton("确定",this);
    ElaPushButton *btnReject = new ElaPushButton("取消",this);

    btnLayout->addStretch();
    btnLayout->addWidget(btnConfirm);
    btnLayout->addSpacing(50);
    btnLayout->addWidget(btnReject);
    btnLayout->addStretch();

    mainLayout->addStretch();
    mainLayout->addLayout(nameLayout);
    mainLayout->addLayout(idLayout);
    mainLayout->addSpacing(35);
    mainLayout->addLayout(btnLayout);
    mainLayout->addStretch();




    connect(contentName, &ElaLineEdit::textChanged, this, [=](){
        // 如果 contentId 有项，则清空，准备更新
        if(contentId->count() != 0) {
            contentId->clear();  // 清空现有编号，准备更新
        }

        // QString patientFile = "./../../../Patient_Document/Patient/";
        QString patientFile = "resource/patients/patient/";
        QString getName = contentName->text();
        patientFile = patientFile + getName + ".txt";  // 根据姓名加载文件路径

        qDebug() << "T_widget:" + patientFile;

        // 检查文件是否存在
        if(QFile::exists(patientFile)) {
            qDebug() << "T_widget: [" + patientFile + "] file Found";
            QFile pf(patientFile);
            pf.open(QIODevice::ReadOnly);
            QTextStream pin(&pf);
            QString pline;
            QString pcurrentRegion = "";

            while(!pin.atEnd()) {
                pline = pin.readLine();
                if(pline.startsWith('[') && pline.endsWith(']')) {
                    pcurrentRegion = pline;
                    continue;
                }
                if(pcurrentRegion == "[name]") {
                    contentName->setText(pline);  // 设置姓名
                }
                else if(pcurrentRegion == "[Id]") {
                    contentId->addItem(pline);  // 将从文件读取的病人编号添加到下拉框
                }
            }
            pf.close();
        } else {
            qDebug() << "T_widget: [" + patientFile + "] file Not Found";
        }

    });

    connect(btnConfirm,&ElaPushButton::clicked,this,&T_Widget::onClicked);
    connect(btnReject,&ElaPushButton::clicked,this,[=]()
    {
        contentId->clear();
        contentName->clear();
        close();
    });
}


void T_Widget::onClicked()
{
    // fileName = "./../../../Patient_Document/Document/";
    // QString photoName("./../../../Patient_Document/Document/");
    fileName = "resource/patients/document/";
    QString photoName("resource/patients/document/");
    QString getName;
    QString getId;
    getName = contentName->text();
    getId = contentId->currentText();
    if(getName == "")
    {
        ElaMessageBar::error(ElaMessageBarType::Top,"错误", "请填写病人姓名!", 2000);
    }
    else if(getId == "")
    {
        ElaMessageBar::error(ElaMessageBarType::Top,"错误", "请填写病人编号!", 2000);
    }
    else
    {
        fileName = fileName+getName+'_'+getId+".txt";
        photoName = photoName+getName+'_'+getId+".jpg";
        QFile file(fileName);
        if(QFile::exists(fileName))
        {
            qDebug()<<fileName+"wenjiancunzai!!!!!!!!!!";
            _name = getName;
            _Id = getId;
            image.load(photoName);
            file.open(QIODevice::ReadOnly);
            QTextStream in(&file);
            QString line;
            QString currentRegion = "";
            while(!in.atEnd())
            {
                line = in.readLine();
                if(line.startsWith('[') && line.endsWith(']'))
                {
                    currentRegion = line;
                    continue;
                }
                if(currentRegion == "[gender]")
                {
                    _gender = line;
                }else if(currentRegion == "[age]")
                {
                    _age = line;
                }else if(currentRegion == "[zone]")
                {
                    _zone = line;
                }else if(currentRegion == "[date]")
                {
                    _date = line;
                }else if(currentRegion == "[diagnosis]")
                {
                    _diagnosis = line;
                }else if(currentRegion == "[bodycheck]")
                {
                    _bodycheck = line;
                }else if(currentRegion == "[complaint]")
                {
                    _complaint = line;
                }else if(currentRegion == "[presenthistory]")
                {
                    _presenthistory = line;
                } else if(currentRegion == "[anamnesis]")
                {
                    _anamnesis = line;
                }else if(currentRegion == "[time]")
                {
                    timeVec.push_back(line);
                }
                else if(currentRegion == "[manner]")
                {
                    contentVec.push_back(line);
                }
                else if(currentRegion == "[notes]")
                {
                    _notes = line;
                }
                file.close();
            }
            emit isSelected();
            contentId->clear();
            contentName->clear();
            timeVec.clear();
            contentVec.clear();
            close();
        }
        else
        {
            ElaMessageBar::error(ElaMessageBarType::Top,"错误", "未找到病人资料!", 2000);
        }
    }
}
